#include <term.h>
#include <inttypes.h>
#include <flash.h>
#include <usart.h>
#include <stdlib.h>
#include <motor.h>

#define ESC 0x1B /* 27d - escape */ 

#define VT100_CUR_HOME_LEN 4
char VT100_CUR_HOME[VT100_CUR_HOME_LEN] __FLASH__ = {ESC,'[',';','H'};

#define VT100_ENABLE_LINEWRAP_LEN 5
char VT100_ENABLE_LINEWRAP[VT100_ENABLE_LINEWRAP_LEN] __FLASH__ = {ESC,'[','?','7','h'};

#define VT100_BACKSPACE_LEN 7
char VT100_BACKSPACE[VT100_BACKSPACE_LEN] __FLASH__ = {ESC,'[','D',' ',ESC,'[','D'};

#define VT100_ESC_BRACKET_LEN 2
char VT100_ESC_BRACKET[VT100_ESC_BRACKET_LEN] __FLASH__ = {ESC,'['};

#define VT100_ERASE_SCREEN_LEN 4
char VT100_ERASE_SCREEN[VT100_ERASE_SCREEN_LEN] __FLASH__ = {ESC,'[','2','J'};

#define VT100_RESET_LEN 2
char VT100_RESET[VT100_RESET_LEN] __FLASH__ = {ESC,'c'};

#define message1_len 6
char message1[message1_len+1] __FLASH__ = "Prudy:";

#define message2_len 7
char message2[message2_len+1] __FLASH__ = "Otacky:";

int8_t omega_edit; // znamienko
int p = 0;

void gotoPOS(uint8_t x,uint8_t y) {
	char temp[8] = {ESC,'['};
	uint8_t i = 2;

	if (y >= 10)
		temp[i++] = (y / 10) + '0';
	temp[i++] = (y % 10) + '0';
	temp[i++] = ';';
	if (x >= 10)
		temp[i++] = (x / 10) + '0';
	temp[i++] = (x % 10) + '0';
	temp[i++] = 'H';
	USART1_DMAsend(temp,i);
}

uint8_t num[4];

void term_init(void) {
	USART1_DMAsend(VT100_RESET,VT100_RESET_LEN);
	omega_edit = 1;
	p = 0;
	num[0] = '0';
	num[1] = '0';
	num[2] = '0';
	num[3] = '0';
}

void term_rewrite(void) {
	uint8_t i;

	gotoPOS(11,9);
	USART1_DMAsend("  ",2);
	if (p == 0 || p == 0)
		USART1_DMAputc('_');
	if (!(num[0] == '0'))
		USART1_DMAputc(num[0]);
	else
		USART1_DMAputc(' ');
	if (p == 0 || p == 1)
		USART1_DMAputc('_');
	if (!(num[0] == '0' && num[1] == '0'))
		USART1_DMAputc(num[1]);
	else
		USART1_DMAputc(' ');
	if (p == 1 || p == 2)
		USART1_DMAputc('_');
	if (!(num[0] == '0' && num[1] == '0' && num[2] == '0'))
		USART1_DMAputc(num[2]);
	else
		USART1_DMAputc(' ');
	if (p == 2 || p == 3)
		USART1_DMAputc('_');
	USART1_DMAputc(num[3]);
	if (p == 3 || p == 3)
		USART1_DMAputc('_');
	USART1_DMAsend(" min^-1",7);
	gotoPOS(1,1);
}

void term_thread(void) {

	if (usart_rx_xfernum) {
		uint8_t ch = getChar();

		switch(ch) {
			case 'R':
				ramp_on = 1;
				break;
			case 'r':
				ramp_on = 0;
				break;
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				num[p] = ch;
				if (p != 3)
					p++;
				break;
			case 'H':
			case 'h':
			case 'D':
				if (p != 0)
					p--;
				break;
			case 'L':
			case 'l':
			case 'C':
				if (p != 3)
					p++;
				break;
			case '+':
				if (omega_edit < 0)
					omega_edit = -omega_edit;
				break;
			case '-':
				if (omega_edit > 0)
					omega_edit = -omega_edit;
				break;
			case 'q':
			case 'Q':
				rampnum = 1;
				break;
			case 'w':
			case 'W':
				rampnum = 2;
				break;
			case 'e':
			case 'E':
				rampnum = 5;
				break;
			case 0x0D: // enter
				omega_newdes = omega_edit*6*((num[0]-'0')*1000+(num[1]-'0')*100+(num[2]-'0')*10+(num[3]-'0'));
				break;
		}
		term_rewrite();
	}
}

